import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import java.util.Scanner;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.imageio.ImageIO;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;


import com.mortennobel.imagescaling.ResampleOp;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public final class test 
{
	
	public test(){
		//System.out.println(1/2);
//		Scanner scan = new Scanner(System.in);
//		String str = "";
//		System.out.println("請輸入：");
		String SQL="insert into student_info (student_number,student_name,student_gender,student_password) values ('" +
				"test number" + "','" +
				"test name" + "'," +
				1 + ",'" + 
				"psd" + "')";
//		str = scan.next();
		String str = "Dazzling cafe'";
		String lat = "25065802353246166";
//		System.out.println(lat.substring(0,8));
		//System.out.println(new SimpleDateFormat("HH").format(new Date())+new SimpleDateFormat("mm").format(new Date()));
		System.out.println(getAddr("台北市大同區迪化街126號(台北車站)"));
	}
	
	private String getAddr(String str){
		String addr = "";
		if(str.contains("區")){
			addr = str.substring(str.indexOf("區")+1,str.length());
		}else if(str.contains("鄉")){
			addr = str.substring(str.indexOf("鄉")+1,str.length());
		}else if(str.contains("鎮")){
			addr = str.substring(str.indexOf("鎮")+1,str.length());
		}else if(str.contains("市")){
			addr = str.substring(str.indexOf("市")+1,str.length());
		}
		if(addr.contains("("))
			addr = addr.substring(0,addr.indexOf("("));
		return addr;
	}
	
	private String getPWD(){
		String pwd="";
		
		for(int i=0;i<10;i++){
			int random = (int)(Math.random()*3 + 1);
			int rnd = 0;
			switch(random){
			case 1:
				rnd = (int)(Math.random()*10 + 48);
				pwd += (char)rnd;
				break;
			case 2:
				rnd = (int)(Math.random()*26 + 97);
				pwd += (char)rnd;
				break;
			case 3:
				rnd = (int)(Math.random()*26 + 65);
				pwd += (char)rnd;
				break;
			default:
				break;
			}
		}
		
		
		return pwd;
	}
	
	
	private void postMail(String subject, String content){
		String mail="austin@ibosscom.com,shindilee@gmail.com";
//
//		File f =  new File("property/log.txt");
//		if(f.exists()){
//			System.out.println(f.length());
//		}
		//System.out.println(strDate);
		try{			
			URL url = new URL("http://test.smarttaxi.com.tw/mgt/nadverapi/mail_common.jsp");
		       
	        HttpURLConnection urlConn = null;
	        urlConn = (HttpURLConnection) url.openConnection();
	        urlConn.setDoInput (true);
	        urlConn.setDoOutput (true);
	        urlConn.setUseCaches (false);
	        urlConn.setRequestMethod("POST");
	        urlConn.connect();
	        DataOutputStream output = null;
	        DataInputStream input = null;
	        output = new DataOutputStream(urlConn.getOutputStream());
	        String para =
	          "email=" + URLEncoder.encode(mail) +
	          "&title=" + URLEncoder.encode(subject)+
	          "&content=" + URLEncoder.encode(content);
	
	        output.writeBytes(para);
	        output.flush();
	        output.close();
	        String str = null;
	
	        input = new DataInputStream (urlConn.getInputStream());
	        while (null != ((str = input.readLine()))) {
	            System.out.println(str);
	        }
	        input.close ();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void doHttpGet(String strurl){  

    }
	
	private void sentMail(){
		final String username = "austin@ibosscom.com";  
        final String password = "shindiiboss";  
        
        String subject = "Austin再次Mail中文測試";
        String content = "中文測試內容!!\n";
        content+="第二行中文測試\n";
  
        Properties props = new Properties();  
        props.put("mail.smtp.host", "smtp.gmail.com");  
        props.put("mail.smtp.socketFactory.port", "465");  
        props.put("mail.smtp.socketFactory.class",  
                "javax.net.ssl.SSLSocketFactory");  
        props.put("mail.smtp.auth", "true");  
        props.put("mail.smtp.port", "465");  
        
        Session session = Session.getInstance(props, new Authenticator() {  
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication(username, password);  
            }  
        });  
  
        try {  
  
            Message message = new MimeMessage(session);  
            message.setFrom(new InternetAddress("austin@ibosscom.com"));  
            message.setRecipients(Message.RecipientType.TO, InternetAddress  
                    .parse("austin@ibosscom.com,shindilee@gmail.com,willbur@ibosscom.com"));  
            message.setSubject(subject);  
            message  
                    .setText(content);  
  
            Transport.send(message);  

        } catch (MessagingException e) {  

        }  
	}
	
	private void way4(String[] nu,boolean haveBack, int tikCount, String totmoney,int doub){
		JFrame f = new JFrame();
		f.setSize(640,1550);
		f.setLayout(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		JPanel jp = new JPanel();
		jp.setLayout(null);
		jp.setBounds(0, 0, 612, 1550);
		jp.setVisible(true);
		
		JLabel txt2=new JLabel(tikCount+"注  "+ totmoney + "元");
		txt2.setFont(new Font("新細明體",Font.PLAIN,20));
		txt2.setBounds(100, 300, 200, 20);
		txt2.setVisible(true);
		jp.add(txt2);
		
		if(doub > 1 && doub != 15 && doub != 20){
			JLabel txt3=new JLabel("＊");
			txt3.setFont(new Font("新細明體",Font.BOLD,23));
			int d = doub-2;
			txt3.setBounds(88+(d*49), 1375, 25, 25);
			txt3.setVisible(true);
			jp.add(txt3);
		}
		if(doub == 15 || doub == 20){
			JLabel txt3=new JLabel("＊");
			txt3.setFont(new Font("新細明體",Font.BOLD,23));
			int d = doub-15;
			txt3.setBounds(530+(d*10), 1375, 25, 25);
			txt3.setVisible(true);
			jp.add(txt3);
		}
		
		for(int i=0; i<((nu.length<=5)?nu.length:5); i++){
			String[] num=nu[i].split(",");
			
			int lx=40;
			int ly=440+(i*186);
			for(int j=0; j<num.length; j++){
				int p=Integer.parseInt(num[j]);
				
				if(p<=10){
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(p*49), ly, 25, 25);
					jp.add(point10);
				}
				if(p>10 && p<=20){
					int temp=p%10;
					if(temp==0)temp=10;
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(temp*49), ly+40,25,25);
					jp.add(point10);
				}
				if(p>20 && p<=30){
					int temp=p%10;
					if(temp==0)temp=10;
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(temp*49), ly+80, 25, 25);
					jp.add(point10);
				}
				if(p>30 && p<=40){
					int temp=p%10;
					if(temp==0)temp=10;
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(temp*49), ly+120, 25, 25);
					jp.add(point10);
				}
				
			}
			
			JLabel txt=new JLabel(nu[i].replace(",", " "));
			txt.setFont(new Font("Arial",Font.PLAIN,18));
			txt.setBounds(480, ly+152, 200, 20);
			txt.setVisible(true);
			jp.add(txt);

		}
		

		TicketPan bkimg = new TicketPan();	
		if(haveBack)
			bkimg.setImage("5391.jpg");
		else
			bkimg.setImage("539_white.jpg");
		bkimg.setVisible(true);
		bkimg.setBounds(0, 0, 612, 1550);
		jp.add(bkimg);
		jp.repaint();

		f.add(jp);
		f.repaint();

		

		BufferedImage bi = ScreenImage.createImage(jp);

		try {			
			ScreenImage.writeImage(bi, "panel3.png");			
		} catch (IOException e) {
			e.printStackTrace();
		}

		//f.dispose();
		/*
		try {
			double[] size = imgSizeZoom();
			
            InputStream is = new BufferedInputStream(
                new FileInputStream("panel3.png"));

            DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;   
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(OrientationRequested.LANDSCAPE);

            PrintService service = PrintServiceLookup.lookupDefaultPrintService();
            Canvas canvas = new Canvas();
            canvas.setImagePath("panel3.png");        
            PrinterJob pj = PrinterJob.getPrinterJob();
            
            PageFormat pf = pj.getPageFormat(null);
            Paper p = pf.getPaper();
            
            int mode=1;
            switch(mode){
            case 1:            	
                pj.setPrintService(service);                                             
                p.setImageableArea(pf.getWidth()-size[0], 0.0, size[0], size[1]);
                pf.setPaper(p);                              
                pj.setPrintable(canvas,pf);
                pj.print();            	
            	break;
            }
            
            is.close();


        } catch (IOException e) {
        	e.printStackTrace();

        } catch (PrinterException e) {
			e.printStackTrace();

        }
        */
	}
	
	private void way3(String[] nu,String[] code2,boolean haveBack, int tikCount, String totmoney, int doub){
		JFrame f = new JFrame();
		f.setSize(640,1550);
		f.setLayout(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//f.hide();

		f.setVisible(true);
		JPanel jp = new JPanel();
		jp.setBounds(0,0,626,1550);
		
		JLabel txt2=new JLabel(tikCount+"注  "+ totmoney + "元");
		txt2.setFont(new Font("新細明體",Font.PLAIN,20));
		txt2.setBounds(100, 100, 200, 20);
		txt2.setVisible(true);
		jp.add(txt2);
		
		if(doub > 1 && doub != 15 && doub != 20){
			JLabel txt3=new JLabel("＊");
			txt3.setFont(new Font("新細明體",Font.BOLD,23));
			int d = doub-2;
			txt3.setBounds(37+(d*50), 1375, 25, 25);
			txt3.setVisible(true);
			jp.add(txt3);
		}
		if(doub == 15 || doub == 20){
			JLabel txt3=new JLabel("＊");
			txt3.setFont(new Font("新細明體",Font.BOLD,23));
			int d = doub-15;
			txt3.setBounds(490+(d*11), 1375, 25, 25);
			txt3.setVisible(true);
			jp.add(txt3);
		}
		
		for(int i=0; i<((nu.length<=4)?nu.length:4); i++){
			String[] num=nu[i].split(",");
			String[] spc=code2[i].split(",");
			
			int lx=0;
			int ly=0;
			if(i<2){
				lx=43+(i*300);
				ly=292;
			}else{
				int temp = i-2;
				lx=43+(temp*300);
				ly=850;
			}
			
			for(int j=0; j<num.length; j++){
				int p=Integer.parseInt(num[j]);
				
				if(p<=6){
					int temp=p-1;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(lx+(temp*50), ly, 17, 17);
					jp.add(point10);
				}
				if(p>6 && p<=12){
					int temp=p%6-1;
					if(temp<0)temp=5;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(lx+(temp*50), ly+40, 17, 17);
					jp.add(point10);
				}
				if(p>12 && p<=18){
					int temp=p%6-1;
					if(temp<0)temp=5;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(lx+(temp*50), ly+80, 17, 17);
					jp.add(point10);
				}
				if(p>18 && p<=24){
					int temp=p%6-1;
					if(temp<0)temp=5;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(lx+(temp*50), ly+120, 17, 17);
					jp.add(point10);
				}
				if(p>24 && p<=30){
					int temp=p%6-1;
					if(temp<0)temp=5;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(lx+(temp*50), ly+160, 17, 17);
					jp.add(point10);
				}
				if(p>30 && p<=36){
					int temp=p%6-1;
					if(temp<0)temp=5;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(lx+(temp*50), ly+200, 17, 17);
					jp.add(point10);
				}
				if(p>36 && p<=42){
					int temp=p%6-1;
					if(temp<0)temp=5;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(lx+(temp*50), ly+240, 17, 17);
					jp.add(point10);
				}
				
				
			}
			
			int spc_x=0;
			int spc_y=0;
			if(i<2){
				spc_x=90+(i*300);
				spc_y=605;		
			}else{
				int temp = i-2;
				spc_x=90+(temp*305);
				spc_y=1165;
			}
			
			
			for(int j=0;j<spc.length;j++){
				int p=Integer.parseInt(spc[j]);
				
				if(p<=4){
					int temp=p-1;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(spc_x+(temp*50), spc_y, 17, 17);
					jp.add(point10);
				}
				if(p>4 && p<=8){
					
					int temp=p%4-1;
					if(temp<0)temp=3;
					TicketPan point10 = new TicketPan();
					point10.setImage("point.jpg");
					point10.setVisible(true);
					point10.setBounds(spc_x+(temp*50), spc_y+40, 17, 17);
					jp.add(point10);
				}
				
			}
			
			
			JLabel txt=new JLabel(nu[i].replace(",", " ")+" 特"+ code2[i]);
			txt.setFont(new Font("新細明體",Font.PLAIN,18));
			txt.setBounds(lx+100, ly+387, 200, 20);
			txt.setVisible(true);
			jp.add(txt);
		}
		
		int y=276;	
		int x=0;
		for(int i=1; i<31; i++){	

			TicketPan point = new TicketPan();
			point.setImage("point.jpg");
			point.setVisible(true);
			point.setBounds(x, y, 22, 8);
			jp.add(point);
			
			if(i==8||i==11||i==21||i==24||i==28)y+=32;
			else y+=40;
			
			if(i==13)y+=56;
			if(i==26)y+=24;
		}
		
		TicketPan point2 = new TicketPan();
		point2.setImage("point.jpg");
		point2.setVisible(true);
		point2.setBounds(33, 1454, 33, 12);
		jp.add(point2);
		
		TicketPan point3 = new TicketPan();
		point3.setImage("point.jpg");
		point3.setVisible(true);
		point3.setBounds(235, 1455, 33, 12);
		jp.add(point3);
		
		TicketPan point4 = new TicketPan();
		point4.setImage("point.jpg");
		point4.setVisible(true);
		point4.setBounds(285, 1455, 33, 12);
		jp.add(point4);
		
		TicketPan point5 = new TicketPan();
		point5.setImage("point.jpg");
		point5.setVisible(true);
		point5.setBounds(385, 1455, 33, 12);
		jp.add(point5);
		
		TicketPan point6 = new TicketPan();
		point6.setImage("point.jpg");
		point6.setVisible(true);
		point6.setBounds(437, 1455, 33, 12);
		jp.add(point6);
		
		TicketPan point7 = new TicketPan();
		point7.setImage("point.jpg");
		point7.setVisible(true);
		point7.setBounds(487, 1455, 33, 12);
		jp.add(point7);
		
		TicketPan point8 = new TicketPan();
		point8.setImage("point.jpg");
		point8.setVisible(true);
		point8.setBounds(27, 1512, 20, 20);
		jp.add(point8);
		
		TicketPan point9 = new TicketPan();
		point9.setImage("point.jpg");
		point9.setVisible(true);
		point9.setBounds(605, 1513, 20, 20);
		jp.add(point9);
		
		
		TicketPan bkimg = new TicketPan();	
		
		bkimg.setImage("superlotto638.jpg");

		bkimg.setVisible(true);
		bkimg.setBounds(0, 0, 626, 1550);
		jp.add(bkimg);
		//jp.repaint();
		

		f.add(jp);
		f.repaint();

		
		BufferedImage bi = ScreenImage.createImage(jp);

		try {			
			ScreenImage.writeImage(bi, "panel2.png");			
		} catch (IOException e) {
			e.printStackTrace();
		}


		
	}
	
	private void way2(String[] nu,boolean haveBack, int tikCount, String totmoney, int doub){
		JFrame f = new JFrame();
		//f.setSize(1,1);
		f.setLayout(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//f.hide();
		f.setAlwaysOnTop(false);
		f.setVisible(true);
		JPanel jp = new JPanel();
		jp.setBounds(0, 0, 640, 1550);
		//jp.setVisible(true);
		
		JLabel txt2=new JLabel(tikCount+"注  "+ totmoney + "元");
		txt2.setFont(new Font("新細明體",Font.PLAIN,19));
		txt2.setBounds(100, 100, 200, 20);
		txt2.setVisible(true);
		jp.add(txt2);
		
		if(doub > 1 && doub != 15 && doub != 20){
			JLabel txt3=new JLabel("＊");
			txt3.setFont(new Font("新細明體",Font.BOLD,23));
			int d = doub-2;
			txt3.setBounds(92+(d*51), 1375, 25, 25);
			txt3.setVisible(true);
			jp.add(txt3);
		}
		if(doub == 15 || doub == 20){
			JLabel txt3=new JLabel("＊");
			txt3.setFont(new Font("新細明體",Font.BOLD,23));
			int d = doub-15;
			txt3.setBounds(550+(d*10), 1375, 25, 25);
			txt3.setVisible(true);
			jp.add(txt3);
		}
		
		for(int i=0; i<((nu.length<=5)?nu.length:5); i++){
			String[] num=nu[i].split(",");
			
			int lx=42;
			int ly=220+(i*233);
			for(int j=0; j<num.length; j++){
				int p=Integer.parseInt(num[j]);
				
				if(p<=10){
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(p*51), ly, 25, 25);
					jp.add(point10);
				}
				if(p>10 && p<=20){
					int temp=p%10;
					if(temp==0)temp=10;
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(temp*51), ly+40,25,25);
					jp.add(point10);
				}
				if(p>20 && p<=30){
					int temp=p%10;
					if(temp==0)temp=10;
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(temp*51), ly+80, 25, 25);
					jp.add(point10);
				}
				if(p>30 && p<=40){
					int temp=p%10;
					if(temp==0)temp=10;
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(temp*51), ly+120, 25, 25);
					jp.add(point10);
				}
				if(p>40 && p<=50){
					int temp=p%10;
					JLabel point10 = new JLabel("＊");
					point10.setFont(new Font("新細明體",Font.BOLD,23));
					point10.setVisible(true);
					point10.setBounds(lx+(temp*51), ly+160, 25, 25);
					jp.add(point10);
				}
				
			}
			
			JLabel txt=new JLabel(nu[i].replace(",", " "));
			txt.setFont(new Font("Arial",Font.BOLD,18));
			txt.setBounds(420, ly+195, 200, 20);
			txt.setVisible(true);
			jp.add(txt);
		}
		
		
		
		int y=206;	
		int x=0;
		for(int i=1; i<35; i++){	

			TicketPan point = new TicketPan();
			point.setImage("point.jpg");
			point.setVisible(true);
			point.setBounds(x, y, 22, 8);
			jp.add(point);
			if(i%6==0 && i <30)y+=32;
			else if(i<30)y+=40;			
			if(i==17)x++;
			if(i==22)x++;
			if(i==25)x+=2;
			if(i==30)y+=31;
			if(i==31)y+=40;
			if(i==32)y+=32;
			if(i==33)y+=40;
		}
		

		
		TicketPan point2 = new TicketPan();
		point2.setImage("point.jpg");
		point2.setVisible(true);
		point2.setBounds(38, 1455, 33, 12);
		jp.add(point2);
		
		TicketPan point3 = new TicketPan();
		point3.setImage("point.jpg");
		point3.setVisible(true);
		point3.setBounds(292, 1455, 33, 12);
		jp.add(point3);
		
		TicketPan point4 = new TicketPan();
		point4.setImage("point.jpg");
		point4.setVisible(true);
		point4.setBounds(394, 1455, 33, 12);
		jp.add(point4);
		
		TicketPan point5 = new TicketPan();
		point5.setImage("point.jpg");
		point5.setVisible(true);
		point5.setBounds(445, 1455, 33, 12);
		jp.add(point5);
		
		TicketPan point6 = new TicketPan();
		point6.setImage("point.jpg");
		point6.setVisible(true);
		point6.setBounds(497, 1455, 33, 12);
		jp.add(point6);
		
		TicketPan point7 = new TicketPan();
		point7.setImage("point.jpg");
		point7.setVisible(true);
		point7.setBounds(548, 1455, 33, 12);
		jp.add(point7);
		
		TicketPan point8 = new TicketPan();
		point8.setImage("point.jpg");
		point8.setVisible(true);
		point8.setBounds(31, 1513, 20, 20);
		jp.add(point8);
		
		TicketPan point9 = new TicketPan();
		point9.setImage("point.jpg");
		point9.setVisible(true);
		point9.setBounds(618, 1512, 20, 20);
		jp.add(point9);
		
		
		
		
		
		TicketPan bkimg = new TicketPan();	
		if(haveBack)
			bkimg.setImage("lotto.jpg");
		else
			bkimg.setImage("lotto649_white.jpg");
		bkimg.setVisible(true);
		bkimg.setBounds(0, 0, 640, 1550);
		jp.add(bkimg);
		//jp.repaint();
		

		f.add(jp);
		f.repaint();
		f.setVisible(false);
		
		//System.out.println("a=>"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
		BufferedImage bi = ScreenImage.createImage(jp);
		//System.out.println("b=>"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
		try {			
			ScreenImage.writeImage(bi, "panel.png");			
		} catch (IOException e) {
			e.printStackTrace();
		}
		//System.out.println("c=>"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
		f.dispose();
		
		try {
			double[] size = imgSizeZoom();
            InputStream is = new BufferedInputStream(
                new FileInputStream("panel.png"));

            DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;   
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(OrientationRequested.LANDSCAPE);

            PrintService service = PrintServiceLookup.lookupDefaultPrintService();
            Canvas canvas = new Canvas();
            canvas.setImagePath("panel.png");        
            PrinterJob pj = PrinterJob.getPrinterJob();
            
            PageFormat pf = pj.getPageFormat(null);
            Paper p = pf.getPaper();
            
            int mode=1;
            switch(mode){
            case 1:            	
                pj.setPrintService(service);                                             
                p.setImageableArea(pf.getWidth()-size[0], 0.0, size[0], size[1]);
                pf.setPaper(p);                              
                pj.setPrintable(canvas,pf);
                pj.print();            	
            	break;
            }
            
            is.close();


        } catch (IOException e) {
        	e.printStackTrace();

        } catch (PrinterException e) {
			e.printStackTrace();

        }
        
	}
	
	public BufferedImage createImage(JPanel panel) {

	    int w = panel.getWidth();
	    int h = panel.getHeight();
	    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
	    Graphics2D g = bi.createGraphics();
	    panel.paint(g);
	    return bi;
	}
	
	private static double[] imgSizeZoom(){
		double widthDist = 612;//635;//800;//280;//350;
		double heightDist = 1550;//1984;//694;//868;
		double zoom = 0.3696221198156682;//0.3593548387096774;//0.2818421052631579;//0.8052631578947368;//0.6442105263157894;
		widthDist = Math.rint(widthDist * zoom);
		heightDist = Math.rint(heightDist * zoom);
		String imgDist = "panel3.png";
		try {  
            File srcfile = new File(imgDist);  
            if (!srcfile.exists()) {  
                    return null;  
            }  
            BufferedImage src = ImageIO.read(srcfile);  
            
            ResampleOp resampleOp = new ResampleOp((int)widthDist,(int)heightDist);
            resampleOp.setUnsharpenMask(com.mortennobel.imagescaling.AdvancedResizeOp.UnsharpenMask.VerySharp);
            
            BufferedImage rescaled = resampleOp.doFilter(src, null, (int)widthDist,(int)heightDist);
            FileOutputStream out = new FileOutputStream(imgDist);  
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
            encoder.encode(rescaled);  
            out.close();  
            return new double[]{widthDist,heightDist};
	    } catch (Exception ex) {  
	            ex.printStackTrace();  
	            return null;
	    }   
	}
	
	private void way1(){
		 int pixels[] = new int[140 * 140];
	        int pixels2[] = new int[120 * 120];
	      try{
	         File inFile = new File("lotto649.jpg");
	         File inFile2 = new File("point2.jpg");
	         File outFile = new File("test.jpg");
	         BufferedImage image = ImageIO.read(inFile);
	         BufferedImage image2 = ImageIO.read(inFile2);
	         pixels=image.getRGB(0,0,140,140,null,0,140);
	         pixels2=image2.getRGB(0,0,120,120,null,0,120);
	         for(int i=0;i<120;i++)
	           for(int j=0;j<120;j++)
	              pixels[140*(12+i)+(12+j)]=pixels2[120*i+j];
	         image.setRGB(0,0,140,140,pixels,0,140);
	         ImageIO.write(image, "JPG", outFile);
	      }
	      catch(Exception e)
	      {
	    	  System.out.println(e.toString());
	      }
		
	}
	
    public static void main(final String[] args)
    {
    	new test();
    }
    
   
    
}

